'use strict';
/**
 * let user to pick passport strategies
 */
 var path = require('path');
 var util = require('util');
 var yeoman = require('yeoman-generator');
 var ScriptBase = require('../../lib/script-base.js');

 var Generator = module.exports = function(that)
 {
	yeoman.generators.NamedBase.apply(this, arguments);

	var _this = this;
 	var cb = this.async();
	var lang = this.lang;
 	var msg = (lang==='cn') ? '选其他第三方登彔方式(passport strategies)插件(local己经自帶)' : 'Choose other passport strategies (local already included)';
    var list = require('../passport')(lang);

 	this.prompt({
 		type: 'checkbox',
 		name: 'passportModules',
 		message: msg,
 		choices: list
 	}, function(props)
 	{
		var hasMod = function (mod) {
            return props.passportModules.indexOf(mod) !== -1;
		};
		list.forEach(function(_mod_) {
            if (hasMod(_mod_.value)) {
                this.extraNodeModules.push(_mod_);
            }
        });
		cb();
 	}.bind(this));
 };

/**
* extending
*/
util.inherits(Generator, ScriptBase);

/**
 * copy the passport strategies files
 */
Generator.prototype.copyFiles = function()
{
	 var _this = this;
	 var fileName = './package.json';
     var file = require(fileName);
	 _this.extraNodeModules.forEach(function(mod)
 	 {
		 file.dependencies[mod.module] = mod.ver;
	 });

	 this.sourceRoot(path.join(__dirname, '../templates/common/app/config/strategies'));

	 fs.writeFile(fileName, JSON.stringify(file, null, 2), function (err) {
		 if (err) {
			 return _this.log.error(chalk.red(err));
		 }
		 _this.extraNodeModules.forEach(function(mod)
	 	{
			var name = mod.value + '.js';
			_this.copy(
				name,
				path.join(
	                this.env.options.appPath,
	                'config',
					'strategies',
	                name
	            )
			);
		});
	 });
};
